home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / GNU_KIT / DISK8.ZIP / src / makest / variable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-08  |  19.5 KB  |  768 lines

  1. /* Internals of variables for GNU Make.
  2. Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "variable.h"
  22. #include "dep.h"
  23. #include "file.h"
  24.  
  25. #ifdef    __GNUC__
  26. #define    max(a, b) \
  27.   ({ register int __a = (a), __b = (b); __a > __b ? __a : __b; })
  28. #else
  29. #define max(a, b) ((a) > (b) ? (a) : (b))
  30. #endif
  31.  
  32.  
  33. /* Hash table of all global variable definitions.  */
  34.  
  35. #ifndef    VARIABLE_BUCKETS
  36. #define VARIABLE_BUCKETS        523
  37. #endif
  38. #ifndef    PERFILE_VARIABLE_BUCKETS
  39. #define    PERFILE_VARIABLE_BUCKETS    23
  40. #endif
  41. #ifndef    SMALL_SCOPE_VARIABLE_BUCKETS
  42. #define    SMALL_SCOPE_VARIABLE_BUCKETS    13
  43. #endif
  44. static struct variable *variable_table[VARIABLE_BUCKETS];
  45. static struct variable_set global_variable_set
  46.   = { variable_table, VARIABLE_BUCKETS };
  47. static struct variable_set_list global_setlist
  48.   = { 0, &global_variable_set };
  49. struct variable_set_list *current_variable_set_list = &global_setlist;
  50.  
  51. /* The next two describe the variable output buffer.
  52.    This buffer is used to hold the variable-expansion of a line of the
  53.    makefile.  It is made bigger with realloc whenever it is too small.
  54.    variable_buffer_length is the size currently allocated.
  55.    variable_buffer is the address of the buffer.  */
  56.  
  57. static unsigned int variable_buffer_length;
  58. static char *variable_buffer;
  59.  
  60. /* Implement variables.  */
  61.  
  62. /* Define variable named NAME with value VALUE in SET.  VALUE is copied.
  63.    LENGTH is the length of NAME, which does not need to be null-terminated.
  64.    ORIGIN specifies the origin of the variable (makefile, command line
  65.    or environment).
  66.    If RECURSIVE is nonzero a flag is set in the variable saying
  67.    that it should be recursively re-expanded.  */
  68.  
  69. static struct variable *
  70. define_variable_in_set (name, length, value, origin, recursive, set)
  71.      char *name;
  72.      unsigned int length;
  73.      char *value;
  74.      enum variable_origin origin;
  75.      int recursive;
  76.      struct variable_set *set;
  77. {
  78.   register unsigned int i;
  79.   register unsigned int hashval;
  80.   register struct variable *v;
  81.  
  82.   hashval = 0;
  83.   for (i = 0; i < length; ++i)
  84.     HASH (hashval, name[i]);
  85.   hashval %= set->buckets;
  86.  
  87.   for (v = set->table[hashval]; v != 0; v = v->next)
  88.     if (*v->name == *name
  89.     && !strncmp (v->name + 1, name + 1, length - 1)
  90.     && v->name[length] == '\0')
  91.       break;
  92.  
  93.   if (env_overrides && origin == o_env)
  94.     origin = o_env_override;
  95.  
  96.   if (v != 0)
  97.     {
  98.       if (env_overrides && v->origin == o_env)
  99.     /* V came from in the environment.  Since it was defined
  100.        before the switches were parsed, it wasn't affected by -e.  */
  101.     v->origin = o_env_override;
  102.  
  103.       /* A variable of this name is already defined.
  104.      If the old definition is from a stronger source
  105.      than this one, don't redefine it.  */
  106.       if ((int) origin >= (int) v->origin)
  107.     {
  108.       v->value = savestring (value, strlen (value));
  109.       v->origin = origin;
  110.       v->recursive = recursive;
  111.     }
  112.       return v;
  113.     }
  114.  
  115.   /* Create a new variable definition and add it to the hash table.  */
  116.  
  117.   v = (struct variable *) xmalloc (sizeof (struct variable));
  118.   v->name = savestring (name, length);
  119.   v->value = savestring (value, strlen (value));
  120.   v->origin = origin;
  121.   v->recursive = recursive;
  122.   v->expanding = 0;
  123.   v->next = set->table[hashval];
  124.   set->table[hashval] = v;
  125.   return v;
  126. }
  127.  
  128. /* Define a variable in the current variable set.  */
  129.  
  130. struct variable *
  131. define_variable (name, length, value, origin, recursive)
  132.      char *name;
  133.      unsigned int length;
  134.      char *value;
  135.      enum variable_origin origin;
  136.      int recursive;
  137. {
  138.   return define_variable_in_set (name, length, value, origin, recursive,
  139.                  current_variable_set_list->set);
  140. }
  141.  
  142. /* Define a variable in FILE's variable set.  */
  143.  
  144. struct variable *
  145. define_variable_for_file (name, length, value, origin, recursive, file)
  146.      char *name;
  147.      unsigned int length;
  148.      char *value;
  149.      enum variable_origin origin;
  150.      int recursive;
  151.      struct file *file;
  152. {
  153.   return define_variable_in_set (name, length, value, origin, recursive,
  154.                  file->variables->set);
  155. }
  156.  
  157. /* Lookup a variable whose name is a string starting at NAME
  158.    and with LENGTH chars.  NAME need not be null-terminated.
  159.    Returns address of the `struct variable' containing all info
  160.    on the variable, or nil if no such variable is defined.  */
  161.  
  162. struct variable *
  163. lookup_variable (name, length)
  164.      char *name;
  165.      unsigned int length;
  166. {
  167.   register struct variable_set_list *setlist;
  168.  
  169.   register unsigned int i;
  170.   register unsigned int rawhash = 0;
  171.  
  172.   for (i = 0; i < length; ++i)
  173.     HASH (rawhash, name[i]);
  174.  
  175.   for (setlist = current_variable_set_list;
  176.        setlist != 0; setlist = setlist->next)
  177.     {
  178.       register struct variable_set *set = setlist->set;
  179.       register unsigned int hashval = rawhash % set->buckets;
  180.       register struct variable *v;
  181.  
  182.       for (v = set->table[hashval]; v != 0; v = v->next)
  183.     if (*v->name == *name
  184.         && !strncmp (v->name + 1, name + 1, length - 1)
  185.         && v->name[length] == 0)
  186.       return v;
  187.     }
  188.  
  189.   return 0;
  190. }
  191.  
  192. /* Initialize FILE's variable set list.  If FILE already has a variable set
  193.    list, the topmost variable set is left intact, but the the rest of the
  194.    chain is replaced with FILE->parent's setlist.  */
  195.  
  196. void
  197. initialize_file_variables (file)
  198.      struct file *file;
  199. {
  200.   register struct variable_set_list *l = file->variables;
  201.   if (l == 0)
  202.     {
  203.       l = (struct variable_set_list *)
  204.     xmalloc (sizeof (struct variable_set_list));
  205.       l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  206.       l->set->buckets = PERFILE_VARIABLE_BUCKETS;
  207.       l->set->table = (struct variable **)
  208.     xmalloc (l->set->buckets * sizeof (struct variable *));
  209.       bzero ((char *) l->set->table,
  210.          l->set->buckets * sizeof (struct variable *));
  211.       file->variables = l;
  212.     }
  213.  
  214.   if (file->parent == 0)
  215.     l->next = &global_setlist;
  216.   else
  217.     {
  218.       if (file->parent->variables == 0)
  219.     initialize_file_variables (file->parent);
  220.       l->next = file->parent->variables;
  221.     }
  222. }
  223.  
  224. /* Pop the top set off the current variable set list,
  225.    and free all its storage.  */
  226.  
  227. void
  228. pop_variable_scope ()
  229. {
  230.   register struct variable_set_list *setlist = current_variable_set_list;
  231.   register struct variable_set *set = setlist->set;
  232.   register unsigned int i;
  233.  
  234.   current_variable_set_list = setlist->next;
  235.   free ((char *) setlist);
  236.  
  237.   for (i = 0; i < set->buckets; ++i)
  238.     {
  239.       register struct variable *next = set->table[i];
  240.       while (next != 0)
  241.     {
  242.       register struct variable *v = next;
  243.       next = v->next;
  244.  
  245.       free (v->name);
  246.       free ((char *) v);
  247.     }
  248.     }
  249.   free ((char *) set->table);
  250.   free ((char *) set);
  251. }
  252.  
  253. /* Create a new variable set and push it on the current setlist.  */
  254.  
  255. void
  256. push_new_variable_scope ()
  257. {
  258.   register struct variable_set_list *setlist;
  259.   register struct variable_set *set;
  260.  
  261.   set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  262.   set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
  263.   set->table = (struct variable **)
  264.     xmalloc (set->buckets * sizeof (struct variable *));
  265.   bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
  266.  
  267.   setlist = (struct variable_set_list *)
  268.     xmalloc (sizeof (struct variable_set_list));
  269.   setlist->set = set;
  270.   setlist->next = current_variable_set_list;
  271.   current_variable_set_list = setlist;
  272. }
  273.  
  274. /* Merge SET1 into SET0, freeing unused storage in SET1.  */
  275.  
  276. static void
  277. merge_variable_sets (set0, set1)
  278.      struct variable_set *set0, *set1;
  279. {
  280.   register unsigned int bucket1;
  281.  
  282.   for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
  283.     {
  284.       register struct variable *v1 = set1->table[bucket1];
  285.       while (v1 != 0)
  286.     {
  287.       struct variable *next = v1->next;
  288.       unsigned int bucket0;
  289.       register struct variable *v0;
  290.  
  291.       if (set1->buckets >= set0->buckets)
  292.         bucket0 = bucket1;
  293.       else
  294.         {
  295.           register char *n;
  296.           bucket0 = 0;
  297.           for (n = v1->name; *n != '\0'; ++n)
  298.         HASH (bucket0, *n);
  299.         }
  300.       bucket0 %= set0->buckets;
  301.  
  302.       for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
  303.         if (streq (v0->name, v1->name))
  304.           break;
  305.  
  306.       if (v0 == 0)
  307.         {
  308.           /* There is no variable in SET0 with the same name.  */
  309.           v1->next = set0->table[bucket0];
  310.           set0->table[bucket0] = v1;
  311.         }
  312.       else
  313.         {
  314.           /* The same variable exists in both sets.
  315.          SET0 takes precedence.  */
  316.           free (v1->value);
  317.           free ((char *) v1);
  318.         }
  319.  
  320.       v1 = next;
  321.     }
  322.     }
  323. }
  324.  
  325. /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
  326.  
  327. void
  328. merge_variable_set_lists (setlist0, setlist1)
  329.      struct variable_set_list **setlist0, *setlist1;
  330. {
  331.   register struct variable_set_list *list0 = *setlist0;
  332.   struct variable_set_list *last0 = 0;
  333.  
  334.   while (setlist1 != 0 && list0 != 0)
  335.     {
  336.       struct variable_set_list *next = setlist1;
  337.       setlist1 = setlist1->next;
  338.  
  339.       merge_variable_sets (list0->set, next->set);
  340.  
  341.       free ((char *) next);
  342.  
  343.       last0 = list0;
  344.       list0 = list0->next;
  345.     }
  346.  
  347.   if (setlist1 != 0)
  348.     {
  349.       if (last0 == 0)
  350.     *setlist0 = setlist1;
  351.       else
  352.     last0->next = setlist1;
  353.     }
  354. }
  355.  
  356. /* Define the automatic variables, and record the addresses
  357.    of their structures so we can change their values quickly.  */
  358.  
  359. void
  360. define_automatic_variables ()
  361. {
  362.   extern char default_shell[];
  363.   register struct variable *v;
  364.   char buf[100];
  365.  
  366.   sprintf (buf, "%u", makelevel);
  367.   (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
  368.  
  369.   /* This won't override any definition, but it
  370.      will provide one if there isn't one there.  */
  371.   v = define_variable ("SHELL", 5, default_shell, o_default, 0);
  372.  
  373.   /* Don't let SHELL come from the environment
  374.      if MAKELEVEL is 0.  Also, SHELL must not be empty.  */
  375.   if (*v->value == '\0' || (v->origin == o_env && makelevel == 0))
  376.     {
  377.       v->origin = o_file;
  378.       v->value = savestring ("/bin/sh", 7);
  379.     }
  380. }
  381.  
  382. /* Subroutine of variable_expand and friends:
  383.    The text to add is LENGTH chars starting at STRING to the variable_buffer.
  384.    The text is added to the buffer at PTR, and the updated pointer into
  385.    the buffer is returned as the value.  Thus, the value returned by
  386.    each call to variable_buffer_output should be the first argument to
  387.    the following call.  */
  388.  
  389. char *
  390. variable_buffer_output (ptr, string, length)
  391.      char *ptr, *string;
  392.      unsigned int length;
  393. {
  394.   register unsigned int newlen = length + (ptr - variable_buffer);
  395.  
  396.   if (newlen > variable_buffer_length)
  397.     {
  398.       unsigned int offset = ptr - variable_buffer;
  399.       variable_buffer_length = max (2 * variable_buffer_length, newlen + 100);
  400.       variable_buffer = (char *) xrealloc (variable_buffer,
  401.                        variable_buffer_length);
  402.       ptr = variable_buffer + offset;
  403.     }
  404.  
  405.   bcopy (string, ptr, length);
  406.   return ptr + length;
  407. }
  408.  
  409. /* Return a pointer to the beginning of the variable buffer.  */
  410.  
  411. char *
  412. initialize_variable_output ()
  413. {
  414.   /* If we don't have a variable output buffer yet, get one.  */
  415.  
  416.   if (variable_buffer == 0)
  417.     {
  418.       variable_buffer_length = 200;
  419.       variable_buffer = (char *) xmalloc (variable_buffer_length);
  420.     }
  421.  
  422.   return variable_buffer;
  423. }
  424.  
  425. /* Create a new environment for FILE's commands.
  426.    The child's MAKELEVEL variable is incremented.  */
  427.  
  428. char **
  429. target_environment (file)
  430.      struct file *file;
  431. {
  432.   register struct variable_set_list *s;
  433.   struct variable_bucket
  434.     {
  435.       struct variable_bucket *next;
  436.       struct variable *variable;
  437.     };
  438.   struct variable_bucket **table;
  439.   unsigned int buckets;
  440.   register unsigned int i;
  441.   register unsigned nvariables;
  442.   char **result;
  443.  
  444.   int noexport = enter_file (".NOEXPORT")->is_target;
  445.  
  446.   /* Find the lowest number of buckets in any set in the list.  */
  447.   s = file->variables;
  448.   buckets = s->set->buckets;
  449.   for (s = s->next; s != 0; s = s->next)
  450.     if (s->set->buckets < buckets)
  451.       buckets = s->set->buckets;
  452.  
  453.   /* Temporarily allocate a table with that many buckets.  */
  454.   table = (struct variable_bucket **)
  455.     alloca (buckets * sizeof (struct variable_bucket *));
  456.   bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
  457.  
  458.   /* Run through all the variable sets in the list,
  459.      accumulating variables in TABLE.  */
  460.   nvariables = 0;
  461.   for (s = file->variables; s != 0; s = s->next)
  462.     {
  463.       register struct variable_set *set = s->set;
  464.       for (i = 0; i < set->buckets; ++i)
  465.     {
  466.       register struct variable *v;
  467.       for (v = set->table[i]; v != 0; v = v->next)
  468.         {
  469.           unsigned int j = i % buckets;
  470.           register struct variable_bucket *ov;
  471.           register char *p = v->name;
  472.  
  473.           /* If `.NOEXPORT' was specified, only export command-line and
  474.          environment variables.  This is a temporary (very ugly) hack
  475.          until I fix this problem the right way in version 4.  Ick.  */
  476.           if (noexport
  477.           && (v->origin != o_command
  478.               && v->origin != o_env && v->origin != o_env_override
  479.               && !(v->origin == o_file && getenv (p) != 0)))
  480.         continue;
  481.  
  482.           if (v->origin == o_default
  483.           || streq (p, "MAKELEVEL"))
  484.         continue;
  485.  
  486.           if (*p != '_' && (*p < 'A' || *p > 'Z')
  487.           && (*p < 'a' || *p > 'z'))
  488.         continue;
  489.           for (++p; *p != '\0'; ++p)
  490.         if (*p != '_' && (*p < 'a' || *p > 'z')
  491.             && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
  492.           break;
  493.           if (*p != '\0')
  494.         continue;
  495.  
  496.           for (ov = table[j]; ov != 0; ov = ov->next)
  497.         if (streq (v->name, ov->variable->name))
  498.           break;
  499.           if (ov == 0)
  500.         {
  501.           register struct variable_bucket *entry;
  502.           entry = (struct variable_bucket *)
  503.             alloca (sizeof (struct variable_bucket));
  504.           entry->next = table[j];
  505.           entry->variable = v;
  506.           table[j] = entry;
  507.           ++nvariables;
  508.         }
  509.         }
  510.     }
  511.     }
  512.  
  513.   result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
  514.   nvariables = 0;
  515.   for (i = 0; i < buckets; ++i)
  516.     {
  517.       register struct variable_bucket *b;
  518.       for (b = table[i]; b != 0; b = b->next)
  519.     {
  520.       register struct variable *v = b->variable;
  521.       result[nvariables++] = concat (v->name, "=", v->value);
  522.     }
  523.     }
  524.   result[nvariables] = (char *) xmalloc (100);
  525.   (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
  526.   result[++nvariables] = 0;
  527.  
  528.   return result;
  529. }
  530.  
  531. /* Try to interpret LINE (a null-terminated string)
  532.    as a variable definition.  If it is one, define the
  533.    variable and return 1.  Otherwise return 0.
  534.  
  535.    ORIGIN may be o_file, o_override, o_env, o_env_override,
  536.    or o_command specifying that the variable definition comes
  537.    from a makefile, an override directive, the environment with
  538.    or without the -e switch, or the command line.
  539.  
  540.    A variable definition has the form "name = value" or "name := value".
  541.    Any whitespace around the "=" or ":=" is removed.  The first form
  542.    defines a variable that is recursively re-evaluated.  The second form
  543.    defines a variable whose value is variable-expanded at the time of
  544.    definition and then is evaluated only once at the time of expansion.  */
  545.  
  546. int
  547. try_variable_definition (line, origin)
  548.      char *line;
  549.      enum variable_origin origin;
  550. {
  551.   register int c;
  552.   register char *p = line;
  553.   register char *beg;
  554.   register char *end;
  555.   register int recursive;
  556.  
  557.   if (*p == '\t')
  558.     return 0;
  559.   while (1)
  560.     {
  561.       c = *p++;
  562.       if (c == '\0' || c == '#')
  563.     return 0;
  564.       if (c == '=')
  565.     {
  566.       recursive = 1;
  567.       break;
  568.     }
  569.       else if (c == ':')
  570.     if (*p == '=')
  571.       {
  572.         ++p;
  573.         recursive = 0;
  574.         break;
  575.       }
  576.     else
  577.       return 0;
  578.     }
  579.  
  580.   beg = next_token (line);
  581.   end = p - 1;
  582.   if (!recursive)
  583.     --end;
  584.   while (end[-1] == ' ' || end[-1] == '\t')
  585.     --end;
  586.   p = next_token (p);
  587.  
  588.   (void) define_variable (beg, end - beg, recursive ? p : variable_expand (p),
  589.               origin, recursive);
  590.  
  591.   return 1;
  592. }
  593.  
  594. /* Print information for variable V, prefixing it with PREFIX.  */
  595.  
  596. static void
  597. print_variable (v, prefix)
  598.      register struct variable *v;
  599.      char *prefix;
  600. {
  601.   char *origin;
  602.  
  603.   switch (v->origin)
  604.     {
  605.     case o_default:
  606.       origin = "default";
  607.       break;
  608.     case o_env:
  609.       origin = "environment";
  610.       break;
  611.     case o_file:
  612.       origin = "makefile";
  613.       break;
  614.     case o_env_override:
  615.       origin = "environment under -e";
  616.       break;
  617.     case o_command:
  618.       origin = "command line";
  619.       break;
  620.     case o_override:
  621.       origin = "`override' directive";
  622.       break;
  623.     case o_automatic:
  624.       origin = "automatic";
  625.       break;
  626.     case o_invalid:
  627.     default:
  628.       abort ();
  629.       break;
  630.     }
  631.   printf ("# %s\n", origin);
  632.  
  633.   fputs (prefix, stdout);
  634.  
  635.   /* Is this a `define'?  */
  636.   if (v->recursive && index (v->value, '\n') != 0)
  637.     printf ("define %s\n%sendef\n", v->name, v->value);
  638.   else
  639.     {
  640.       register char *p;
  641.  
  642.       printf ("%s %s= ", v->name, v->recursive ? "" : ":");
  643.  
  644.       /* Check if the value is just whitespace.  */
  645.       p = next_token (v->value);
  646.       if (p != v->value && *p == '\0')
  647.     /* All whitespace.  */
  648.     printf ("$(subst ,,%s)", v->value);
  649.       else if (v->recursive)
  650.     fputs (v->value, stdout);
  651.       else
  652.     /* Double up dollar signs.  */
  653.     for (p = v->value; *p != '\0'; ++p)
  654.       {
  655.         if (*p == '$')
  656.           putchar ('$');
  657.         putchar (*p);
  658.       }
  659.       putchar ('\n');
  660.     }
  661. }
  662.  
  663.  
  664. /* Print all the variables in SET.  PREFIX is printed before
  665.    the actual variable definitions (everything else is comments).  */
  666.  
  667. static void
  668. print_variable_set (set, prefix)
  669.      register struct variable_set *set;
  670.      char *prefix;
  671. {
  672.   register unsigned int i, nvariables, per_bucket;
  673.   register struct variable *v;
  674.  
  675.   per_bucket = nvariables = 0;
  676.   for (i = 0; i < set->buckets; ++i)
  677.     {
  678.       register unsigned int this_bucket = 0;
  679.  
  680.       for (v = set->table[i]; v != 0; v = v->next)
  681.     {
  682.       ++this_bucket;
  683.       print_variable (v, prefix);
  684.     }
  685.  
  686.       nvariables += this_bucket;
  687.       if (this_bucket > per_bucket)
  688.     per_bucket = this_bucket;
  689.     }
  690.  
  691.   if (nvariables == 0)
  692.     puts ("# No variables.");
  693.   else
  694.     {
  695.       printf ("# %u variables in %u hash buckets.\n",
  696.           nvariables, set->buckets);
  697. #ifndef    NO_FLOAT
  698.       printf ("# average of %.1f variables per bucket, \
  699. max %u in one bucket.\n",
  700.           ((double) nvariables) * 100.0 / (double) set->buckets,
  701.           per_bucket);
  702. #endif
  703.     }
  704. }
  705.  
  706.  
  707. /* Print the data base of variables.  */
  708.  
  709. void
  710. print_variable_data_base ()
  711. {
  712.   puts ("\n# Variables\n");
  713.  
  714.   print_variable_set (&global_variable_set, "");
  715. }
  716.  
  717.  
  718. /* Print all the local variables of FILE.  */
  719.  
  720. void
  721. print_file_variables (file)
  722.      struct file *file;
  723. {
  724.   if (file->variables != 0)
  725.     print_variable_set (file->variables->set, "# ");
  726. }
  727.  
  728. struct output_state
  729.   {
  730.     char *buffer;
  731.     unsigned int length;
  732.   };
  733.  
  734. /* Save the current variable output state and return a pointer
  735.    to storage describing it.  Then reset the output state.  */
  736.  
  737. char *
  738. save_variable_output ()
  739. {
  740.   struct output_state *state;
  741.  
  742.   state = (struct output_state *) xmalloc (sizeof (struct output_state));
  743.   state->buffer = variable_buffer;
  744.   state->length = variable_buffer_length;
  745.  
  746.   variable_buffer = 0;
  747.   variable_buffer_length = 0;
  748.  
  749.   return (char *) state;
  750. }
  751.  
  752. /* Restore the variable output state saved in SAVE.  */
  753.  
  754. void
  755. restore_variable_output (save)
  756.      char *save;
  757. {
  758.   register struct output_state *state = (struct output_state *) save;
  759.  
  760.   if (variable_buffer != 0)
  761.     free (variable_buffer);
  762.  
  763.   variable_buffer = state->buffer;
  764.   variable_buffer_length = state->length;
  765.  
  766.   free ((char *) state);
  767. }
  768.